package com.ruoyi.erp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.erp.domain.ProjectBonus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ProjectBonusConfigMapper;
import com.ruoyi.erp.domain.ProjectBonusConfig;
import com.ruoyi.erp.service.IProjectBonusConfigService;
import com.ruoyi.common.core.text.Convert;

/**
 * 提成比例配置Service业务层处理
 * 
 * @author sauger
 * @date 2022-04-22
 */
@Service
public class ProjectBonusConfigServiceImpl implements IProjectBonusConfigService 
{
    @Autowired
    private ProjectBonusConfigMapper projectBonusConfigMapper;

    private List<ProjectBonusConfig> bonusConfigs;
    /**
     * 查询提成比例配置
     * 
     * @param id 提成比例配置主键
     * @return 提成比例配置
     */
    @Override
    public ProjectBonusConfig selectProjectBonusConfigById(Long id)
    {
        return projectBonusConfigMapper.selectProjectBonusConfigById(id);
    }

    /**
     * 查询提成比例配置列表
     * 
     * @param projectBonusConfig 提成比例配置
     * @return 提成比例配置
     */
    @Override
    public List<ProjectBonusConfig> selectProjectBonusConfigList(ProjectBonusConfig projectBonusConfig)
    {
        return projectBonusConfigMapper.selectProjectBonusConfigList(projectBonusConfig);
    }

    /**
     * 新增提成比例配置
     * 
     * @param projectBonusConfig 提成比例配置
     * @return 结果
     */
    @Override
    public int insertProjectBonusConfig(ProjectBonusConfig projectBonusConfig)
    {
        return projectBonusConfigMapper.insertProjectBonusConfig(projectBonusConfig);
    }

    /**
     * 修改提成比例配置
     * 
     * @param projectBonusConfig 提成比例配置
     * @return 结果
     */
    @Override
    public int updateProjectBonusConfig(ProjectBonusConfig projectBonusConfig)
    {
        return projectBonusConfigMapper.updateProjectBonusConfig(projectBonusConfig);
    }

    /**
     * 批量删除提成比例配置
     * 
     * @param ids 需要删除的提成比例配置主键
     * @return 结果
     */
    @Override
    public int deleteProjectBonusConfigByIds(String ids)
    {
        return projectBonusConfigMapper.deleteProjectBonusConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除提成比例配置信息
     * 
     * @param id 提成比例配置主键
     * @return 结果
     */
    @Override
    public int deleteProjectBonusConfigById(Long id)
    {
        return projectBonusConfigMapper.deleteProjectBonusConfigById(id);
    }

    @Override
    public BigDecimal getBonusRateByProfitRate(BigDecimal profitRate) {
        profitRate = profitRate.multiply(BigDecimal.valueOf(100));
        if (bonusConfigs == null){
            this.bonusConfigs = this.selectProjectBonusConfigList(null);
        }
        BigDecimal ret = BigDecimal.ZERO;
        for(int i=0; i < bonusConfigs.size()-1; i++){
            if(bonusConfigs.get(i).getMaxRate().compareTo(profitRate) <= 0){
                ret = bonusConfigs.get(i).getBonusRate();
            }else {
                break;
            }
        }
        return ret;
    }
}
